#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_STUDENTS 100

// 定义学生成绩结构体
typedef struct {
    char name[50];
    char id[20];
    char course[50];
    float score;
} Student;

Student students[MAX_STUDENTS];
int studentCount = 0;

// 记录学生成绩函数
void recordScore() {
    if (studentCount >= MAX_STUDENTS) {
        printf("学生记录已满，无法添加更多学生。\n");
        return;
    }
    printf("请输入学生姓名：");
    scanf("%s", students[studentCount].name);
    printf("请输入学生学号：");
    scanf("%s", students[studentCount].id);
    printf("请输入课程名称：");
    scanf("%s", students[studentCount].course);
    printf("请输入成绩（0-100）：");
    scanf("%f", &students[studentCount].score);
    studentCount++;
    printf("成绩已成功记录！\n");
}

// 按学生姓名查询成绩函数
void queryByName() {
    char name[50];
    printf("请输入学生姓名：");
    scanf("%s", name);
    int found = 0;
    for (int i = 0; i < studentCount; i++) {
        if (strcmp(students[i].name, name) == 0) {
            printf("姓名：%s, 学号：%s, 课程：%s, 成绩：%.1f\n", 
                   students[i].name, students[i].id, students[i].course, students[i].score);
            found = 1;
        }
    }
    if (!found) {
        printf("未找到该学生的成绩记录。\n");
    }
}

// 按学生学号查询成绩函数
void queryById() {
    char id[20];
    printf("请输入学生学号：");
    scanf("%s", id);
    int found = 0;
    for (int i = 0; i < studentCount; i++) {
        if (strcmp(students[i].id, id) == 0) {
            printf("姓名：%s, 学号：%s, 课程：%s, 成绩：%.1f\n", 
                   students[i].name, students[i].id, students[i].course, students[i].score);
            found = 1;
        }
    }
    if (!found) {
        printf("未找到该学生的成绩记录。\n");
    }
}
//修复 “成绩显示格式” 问题
// 按课程名称查询成绩函数
void queryByCourse() {
    char course[50];
    printf("请输入课程名称：");
    scanf("%s", course);
    int found = 0;
    for (int i = 0; i < studentCount; i++) {
        if (strcmp(students[i].course, course) == 0) {
            printf("姓名：%s, 学号：%s, 课程：%s, 成绩：%.1f\n", 
                   students[i].name, students[i].id, students[i].course, students[i].score);
            found = 1;
        }
    }
    if (!found) {
        printf("未找到该课程的成绩记录。\n");
    }
}

int main() {
    int choice;
    while (1) {
        printf("\n请选择操作：\n");
        printf("1. 记录学生成绩\n");
        printf("2. 查询学生成绩\n");
        printf("3. 退出\n");
        printf("请输入选项序号：");
        scanf("%d", &choice);
        switch (choice) {
            case 1:
                recordScore();
                break;
            case 2:
                printf("请选择查询方式：\n");
                printf("1. 按学生姓名查询\n");
                printf("2. 按学生学号查询\n");
                printf("3. 按课程名称查询\n");
                printf("请输入选项序号：");
                scanf("%d", &choice);
                switch (choice) {
                    case 1:
                        queryByName();
                        break;
                    case 2:
                        queryById();
                        break;
                    case 3:
                        queryByCourse();
                        break;
                    default:
                        printf("无效的查询选项，请重新选择。\n");
                }
                break;
            case 3:
                printf("程序已退出。\n");
                return 0;
            default:
                printf("无效的选项，请重新选择。\n");
        }
    }
    return 0;
}